<template>
  <div class="app-container">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" style="margin-bottom: 20px;">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>博客管理</el-breadcrumb-item>
      <el-breadcrumb-item>博客列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 条件查询   -->
    <el-form :inline="true" :model="query" size="mini" >
      <el-form-item label="文章标题:">
        <el-input v-model.trim="query.title"></el-input>
      </el-form-item>
      <el-form-item label="分类:">
        <el-select v-model="checkedCategoryId" clearable filterable style="width:120px;">
          <el-option v-for="item in categoryList" :key="item.categoryId" :label="item.name" :value="item.categoryId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
        <el-button icon="el-icon-refresh" @click="reload">重置</el-button>
      </el-form-item>
    </el-form>
    <!--  列表数据  -->
    <el-table :data="list" border style="width: 100%">
      <el-table-column align="center" label="序号" width="45" type="index"></el-table-column>
      <el-table-column align="center" prop="title" label="文章标题" width="280"></el-table-column>
      <el-table-column align="center" prop="img" label="主图" width="120">
        <template slot-scope="scope">
          <el-image :src="scope.row.img" style="height: 50px;"></el-image>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="categoryName" label="分类" width="120">
        <template slot-scope="scope">
          <el-tag type="success" >{{scope.row.categoryName}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="thumhup" label="点赞数" width="80" ></el-table-column>
      <el-table-column align="center" prop="status" label="状态" width="120">
        <template slot-scope="scope">
          <!-- 1:已发布 2:草稿 -->
          <el-tag v-if="scope.row.status === 1" type="success">已发布</el-tag>
          <el-tag v-if="scope.row.status === 2" type="danger">草稿</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="ispublic" label="是否公开" width="120">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isPublic === 0" type="warning">不公开</el-tag>
          <el-tag v-if="scope.row.isPublic === 1" type="danger">公开</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="views" label="浏览数" width="120">
        <template slot-scope="scope">
          <el-tag type="primary">{{scope.row.views}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="createTime" label="创建时间" width="180">
        <template slot-scope="scope">
          <!-- 格式化日期 -->
          {{getFormat(scope.row.createTime)}}
        </template>
      </el-table-column>
      <el-table-column  align="center" label="操作">
        <template slot-scope="scope">
          <el-button icon="el-icon-search" type="primary" size="mini" @click="detail(scope.row.articleId)">查看</el-button>
          <el-button icon="el-icon-edit-outline" type="success" size="mini" @click="handleEdit(scope.row.articleId)">编辑</el-button>
          <el-button icon="el-icon-set-up" type="info" size="mini" v-if="scope.row.status === 2" @click="handleRelease(scope.row.articleId)">发布</el-button>
          <el-button icon="el-icon-document-delete" type="danger" size="mini" @click="handleDel(scope.row.articleId)" v-if="scope.row.deleted !== 1">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="query.current"
      :page-sizes="[10, 20, 50]"
      :page-size="query.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="query.total">
    </el-pagination>

    <!-- 编辑或者查看详情窗口 -->
    <edit :id="edit.id" :isEdit="edit.isEdit" :title="edit.title" :visiable="edit.visiable" :remoteClose="remoteClose" />
  </div>
</template>

<script>
// 格式化日期
import {dateFormat} from '@/utils/date';
import blogApi from '@/api/blog';
import Edit from './edit';
import Breadcrumb from '../../components/Breadcrumb';
export default {
  components:{
    Edit, Breadcrumb
  },
  data() {
    return {
      list: [],
      query: {
        current: 1,   // 当前页码
        size: 10,     // 每页大小
        total: 0
      },
      categoryList: [], // 分类列表
      labelList: [], // 标签列表
      checkedCategoryId: '',  // 选中的分类
      checkedLabelId: '', // 选中的标签
      edit:{
        title: '',
        visiable: false,
        isEdit: true,
        id: null,
      },
    }
  },

  created() {
    this.fetchCategory();
    this.fetchData();
  },

  watch: {
    checkedCategoryId (newId, oldId) {
      this.checkedLabelId  = '';
      if (newId) {
        this.fetchLabel(newId);
      }
    }
  },

  methods: {
    // 条件分页查询博客列表
    fetchData() {
      blogApi.queryArticleList(this.query).then(response => {
        if (response.code === 200) {
          this.list = response.data.respVos;
          this.query.total = response.data.size;
        }
      })
    },

    // 查询博客详情
    detail(id) {
      this.edit.visiable = true;
      this.edit.title = '详情'
      this.edit.id = id;
      this.edit.isEdit = false;
    },

    // 编辑
    handleEdit(id) {
      this.edit.visiable = true;
      this.edit.title = '编辑'
      this.edit.id = id;
      this.edit.isEdit = true;
    },

    // 条件查询
    queryData() {
      this.query.labelId = this.checkedLabelId;
      this.query.categoryId = this.checkedCategoryId;
      this.fetchData();
    },

    // val 是切换之后的每页显示多少条
    handleSizeChange(val) {
      this.query.size = val;
      this.fetchData();
    },
    // 当页码改变后触发此方法 val是当前点击（或输入）的页码
    handleCurrentChange(val) {
      this.query.current = val;
      this.fetchData();
    },

    // 删除
    handleDel(id) {
      this.$confirm('确认删除这条记录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 发送删除请求
        blogApi.delete(id).then(response => {
          if (response.code === 200) {
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 1500
            });
          } else if (response.code === 306) {
            this.$notify.error({
              title: '错误',
              message: '您没有操作权限，请联系管理员',
              duration: 1500
            });
          } else {
            this.$notify.error({
              title: '错误',
              message: '系统异常，请稍后再试',
              duration: 1500
            });
          }
          // 刷新列表
          this.fetchData();
        })
      }).catch(() => {
        // 取消删除
      });
    },

    // 发布
    handleRelease(id) {
      this.$confirm('确认发布吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        blogApi.release(id).then(response => {
          if (response.code === 200) {
            this.$notify({
              title: '成功',
              message: '文章发布成功',
              type: 'success',
              duration: 1500
            });
          } else {
            this.$notify.error({
              title: '错误',
              message: '文章发布失败',
              duration: 1500
            });
          }
          // 刷新列表
          this.fetchData();
        })
      }).catch(() => {
        // 取消删除
      });
    },

    // 重置
    reload(){
      this.query.title = '';
      this.query.labelId = '';
      this.query.categoryId = '';
      this.checkedLabelId  = '';
      this.checkedCategoryId = '';
      this.fetchData();
    },

    // 获取所有的分类信息
    fetchCategory() {
      blogApi.getAllCategory().then(response => {
        if (response.code === 200) {
          this.categoryList = response.data;
        }
      })
    },

    // 根据分类信息的id查询标签信息
    fetchLabel(categoryId) {
      blogApi.getLabelByCategoryId(categoryId).then(response => {
        if (response.code === 200) {
          this.labelList = response.data;
        }
      })
    },

    // 格式化日期
    getFormat(date){
      return dateFormat(date);
    },

    // 关闭窗口
    remoteClose(){
      this.edit.visiable = false;
      this.edit.title = ''
      this.edit.id = '';
      this.edit.isEdit = true;
      this.fetchData();
    },
  },
}
</script>

<style scoped>

</style>
